package com.sqx.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.dao.UserMoneyDetailsDao;
import com.sqx.modules.app.entity.UserMoneyDetails;
import com.sqx.modules.app.service.UserMoneyDetailsService;
import com.sqx.modules.app.service.UserMoneyService;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.common.service.CommonInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class UserMoneyDetailsServiceImpl extends ServiceImpl<UserMoneyDetailsDao, UserMoneyDetails> implements UserMoneyDetailsService {



    @Override
    public Result queryUserMoneyDetails(Integer page, Integer limit, Long userId,Integer classify,Integer type) {
        IPage<UserMoneyDetails> page1 = new Page(page, limit);
        QueryWrapper<UserMoneyDetails> queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId);
        if(classify!=null){
            queryWrapper.eq("classify", classify);
        }
        if(type!=null){
            queryWrapper.eq("type", type);
        }
        queryWrapper.orderByDesc("create_time");
        return Result.success().put("data", baseMapper.selectPage(page1, queryWrapper));
    }

    @Override
    public Double monthIncome(String date, Long userId) {
        return baseMapper.monthIncome(date,userId);
    }

    @Override
    public void setParentMoneyDetails(Long userId, Long parentId, BigDecimal money, Integer userLevel) {
        //添加用户邀请人钱包详情
        UserMoneyDetails userMoneyDetails = new UserMoneyDetails();
        userMoneyDetails.setUserId(parentId);
        userMoneyDetails.setType(1);
        userMoneyDetails.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        userMoneyDetails.setClassify(3);
        if (userLevel == 1) {
            userMoneyDetails.setTitle("一级用户分成");
            userMoneyDetails.setMoney(money);
            userMoneyDetails.setContent("一级用户分成金额:" + money);
            userMoneyDetails.setUserLevel(1);
            userMoneyDetails.setRelationId(userId);
            baseMapper.insert(userMoneyDetails);
        } else if (userLevel == 2) {
            userMoneyDetails.setTitle("二级用户分成");
            userMoneyDetails.setMoney(money);
            userMoneyDetails.setContent("二级用户分成金额:" + money);
            userMoneyDetails.setRelationId(userId);
            userMoneyDetails.setUserLevel(2);
            baseMapper.insert(userMoneyDetails);
        }

    }

    @Override
    public BigDecimal getOneProfit(Long userId, Integer level) {

        return baseMapper.getOneProfit(userId, level);

    }
}
